import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  // 测试
  {
    path: "test",
    name: "test",
    component: () => import("@/views/test.vue"),
  },
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/components/Login.vue"),
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/components/Home.vue"),
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        name: "welcome",
        component: () => import("@/components/Welcome.vue"),
      },
      {
        path: "/users",
        name: "users",
        component: () => import("@/components/Users/Users.vue"),
      },
      {
        path: "/rights",
        name: "right",
        component: () => import("@/components/power/Right.vue"),
      },
      {
        path: "/roles",
        name: "roles",
        component: () => import("@/components/power/Roles.vue"),
      },
      {
        path: "/categories",
        name: "categories",
        component: () => import("@/components/goods/Categories.vue"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) {
    return next("/login");
  }
  next();
});

export default router;
